

class TestMuhasabeci  extends Thread {

	Paylasilamayan p ; 
	Paylasilamayan2 p2 ; 

	public TestMuhasabeci(Paylasilamayan p, Paylasilamayan2 p2) {
		this.p = p ; 
		this.p2 = p2 ;
	}

	public void run() {
		try {
			p.put();
		} catch (Exception ex) {
			//ex.printStackTrace();
		}
		p2.put();
		System.out.println( this.getName()+"-->"+p.get() );
		
	}
}



class Paylasilamayan {

	private int deger ; 
	private Object ob  = new Object();
	private Thread beklemekiIsParcacigi ; 

	public void put() throws Exception {
		synchronized(ob) {
			deger ++ ;

			if (deger == 2) {
				System.out.println( Thread.currentThread().getName()+" beklemede" );
				beklemekiIsParcacigi = Thread.currentThread();
				ob.wait(); // is parcacigini bekleme odasina al.
				System.out.println("HELOOOOO");
			}

			if (beklemekiIsParcacigi != null) {
				beklemekiIsParcacigi.interrupt();
			}
			System.out.println(Thread.currentThread().getName()+" -0-");
		}
	}

	public int get () {
		synchronized(ob) {
			if (deger == 4 ) {
				//Thread.currentThread().interrupt();
				//ob.notify();
			}
		}
		return deger ;
	}
}



class Paylasilamayan2 {

	private int deger ; 
	private Object ob  = new Object();

	public void put()  {
		synchronized(ob) {
			deger++ ;
			//System.out.println( "Paylasilamayan2--> "+Thread.currentThread().getName());
		}
	}

	
}


public class TestAnaBir {

	public static void main(String args[]) {
		Paylasilamayan p = new Paylasilamayan();
		Paylasilamayan2 p2 = new Paylasilamayan2();
		
		TestMuhasabeci tm1 = new TestMuhasabeci(p, p2);
		TestMuhasabeci tm2 = new TestMuhasabeci(p, p2);
		TestMuhasabeci tm3 = new TestMuhasabeci(p, p2);
		TestMuhasabeci tm4 = new TestMuhasabeci(p, p2);
		TestMuhasabeci tm5 = new TestMuhasabeci(p, p2);
		TestMuhasabeci tm6 = new TestMuhasabeci(p, p2);

		tm1.start();
		tm2.start();
		tm3.start();
		tm4.start();
		tm5.start();
		tm6.start();


	}
}
